package cn.icanci.ladybird.service.admin.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.pojo.bo.AdminUpdateTicketBO;
import cn.icanci.ladybird.pojo.entity.TicketDetails;
import cn.icanci.ladybird.pojo.entity.TicketDetailsTicketTimeMiddle;
import cn.icanci.ladybird.pojo.entity.TicketTime;
import cn.icanci.ladybird.service.TicketDetailsService;
import cn.icanci.ladybird.service.TicketDetailsTicketTimeMiddleService;
import cn.icanci.ladybird.service.TicketTimeService;
import cn.icanci.ladybird.service.admin.AdminUpdateTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author: icanci
 * @date: Created in 2020/10/6 22:20
 * @classAction: 处理更新数据对象
 */
@Service
public class AdminUpdateTicketServiceImpl implements AdminUpdateTicketService {

    @Autowired
    private TicketDetailsService ticketDetailsService;
    @Autowired
    private TicketDetailsTicketTimeMiddleService ticketDetailsTicketTimeMiddleService;
    @Autowired
    private TicketTimeService ticketTimeService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonData updateHandler(HttpServletRequest request, AdminUpdateTicketBO adminUpdateTicketBO) {
        try {
            // 创建details对象
            TicketDetails ticketDetails = new TicketDetails();
            // 把结果封装到details里面
            ticketDetails.setTicketDetailsId(adminUpdateTicketBO.getTicketDetailsId());
            ticketDetails.setTicketDetailsInfo(adminUpdateTicketBO.getTicketName());
            ticketDetails.setTicketDetailsStatus(Integer.valueOf(adminUpdateTicketBO.getTicketStatus()));
            ticketDetails.setTicketDetailsIsKill(Integer.valueOf(adminUpdateTicketBO.getIsKilled()));
            ticketDetails.setTicketAddressCity(adminUpdateTicketBO.getCity());
            ticketDetails.setTicketAddressVenue(adminUpdateTicketBO.getVenue());
            ticketDetails.setTicketDetailsWeight(Integer.valueOf(adminUpdateTicketBO.getTicketWeight()));
            ticketDetails.setUpdateTime(new Date());
            Integer adminId = (Integer) request.getAttribute("admin_id");
            ticketDetails.setTicketDetailsAdminId(Long.valueOf(adminId));

            System.out.println(ticketDetails);
            // 更新数据
            ticketDetailsService.update(ticketDetails);
            // 设置演出时间
            // 查询演出的时间中间表对象
            List<TicketDetailsTicketTimeMiddle> timeMiddles = ticketDetailsTicketTimeMiddleService.queryByTicketDetailsId(ticketDetails.getTicketDetailsId());
            for (int i = 0; i < timeMiddles.size(); i++) {
                Long ticketTimeMiddleId = timeMiddles.get(i).getTicketTimeMiddleId();
                TicketTime ticketTime = ticketTimeService.queryById(ticketTimeMiddleId);
                if (i == 0) {
                    ticketTime.setTicketTime(DateUtil.parse(adminUpdateTicketBO.getTime1()));
                    ticketTime.setUpdateTime(new Date());
                } else if (i == 1) {
                    ticketTime.setTicketTime(DateUtil.parse(adminUpdateTicketBO.getTime2()));
                    ticketTime.setUpdateTime(new Date());
                } else if (i == 2) {
                    ticketTime.setTicketTime(DateUtil.parse(adminUpdateTicketBO.getTime3()));
                    ticketTime.setUpdateTime(new Date());
                }
                ticketTimeService.update(ticketTime);
            }
        } catch (Exception e) {
            return JsonData.builderError(e.getLocalizedMessage());
        }
        return JsonData.builderSuccess();
    }
}
